//
//  KLMyChannelViewController.m
//  KunLun
//
//  Created by Mac on 2018/1/24.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "KLMyChannelViewController.h"
#import "KLMenuControl.h"
#import "KLMyChannelTableViewCell.h"
@interface KLMyChannelViewController ()
@property(nonatomic, strong) STLabel                           *areaLable;
@property(nonatomic, strong) KLMenuControl                     *allControl;//总业绩
@property(nonatomic, strong) KLMenuControl                     *monthControl;//当月业绩
@property(nonatomic, strong) KLMenuControl                     *canUseControl;//可提现
@property(nonatomic, strong) KLMenuControl                     *allPatnerControl;//区域商家
@property(nonatomic, strong) KLMenuControl                     *insUsePartnerControl;//营业商家
@property(nonatomic, strong) KLMenuControl                     *outOfPatnerControl;//未营业商家

@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@property(nonatomic, strong) STNoresultView                     *noresultView;
@end

@implementation KLMyChannelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的渠道";
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 40)];
    header.backgroundColor = [UIColor whiteColor];
    
    
    self.areaLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)
                                                     text:@"    -----------"
                                                textColor:KL_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    self.areaLable.backgroundColor =  self.tableView.backgroundColor;
    [header addSubview:self.areaLable];
    
    
    self.allControl = [[KLMenuControl alloc] initWithFrame:CGRectMake(0, self.areaLable.bottom, UIScreenWidth / 2, 70) value:@"0" name:@"总业绩" showLine:YES];
   // [header addSubview:self.allControl];
    
    self.monthControl = [[KLMenuControl alloc] initWithFrame:CGRectMake(self.allControl.right, self.areaLable.bottom, UIScreenWidth / 3, 70) value:@"0" name:@"当月业绩" showLine:YES];
  //  [header addSubview:self.monthControl];
    
    self.canUseControl = [[KLMenuControl alloc] initWithFrame:CGRectMake(self.allControl.right, self.areaLable.bottom, UIScreenWidth / 2, 70) value:@"0" name:@"可提现业绩" showLine:NO];
 //   [header addSubview:self.canUseControl];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, self.areaLable.bottom, UIScreenWidth, 0.5)];
    line.backgroundColor = KL_lineColor;
    [header addSubview:line];
    
    self.allPatnerControl = [[KLMenuControl alloc] initWithFrame:CGRectMake(0, line.bottom, UIScreenWidth / 3, 70) value:@"0" name:@"区域内商家" showLine:YES];
    [header addSubview:self.allPatnerControl];
    
    self.insUsePartnerControl = [[KLMenuControl alloc] initWithFrame:CGRectMake(self.allPatnerControl.right, line.bottom, UIScreenWidth / 3, 70) value:@"0" name:@"营业中商家" showLine:YES];

    [header addSubview:self.insUsePartnerControl];
    
    
    self.outOfPatnerControl = [[KLMenuControl alloc] initWithFrame:CGRectMake(self.insUsePartnerControl.right, line.bottom, UIScreenWidth / 3, 70) value:@"0" name:@"未营业商家" showLine:NO];
    
    [header addSubview:self.outOfPatnerControl];
    

    
    UIView * view = [KLUtils headerViewWithTitle:@"商家明细"];
    view.top = self.outOfPatnerControl.bottom;
    view.backgroundColor = self.tableView.backgroundColor;
    [header addSubview:view];
    
    header.height= view.bottom;
    
    self.tableView.tableHeaderView = header;
    
    //
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendMychanelRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.noresultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 220, UIScreenWidth, 400) title:@"暂无合作商家" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.noresultView.hidden = YES;
    [self.tableView addSubview:self.noresultView];
}
- (void)reloadData{
    self.noresultView.hidden = self.dataSouce.count;
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLMyChannelTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLMyChannelTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLMyChannelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.section];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendMychanelRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/channel/channel",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 self.dataSouce = [NSMutableArray new];
                                                 if ([dataDic.allKeys containsObject:@"shop_list"]) {
                                                     NSArray * array = dataDic[@"shop_list"];
                                                     for (NSDictionary * modelDic in array) {
                                                         KLMychnelModel * model = [KLMychnelModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 NSString * allMoney = dataDic[@"total"];
                                                 self.allControl.valueLable.text = allMoney;
                                                 
                                                 NSString * balance = dataDic[@"balance"];
                                                 self.canUseControl.valueLable.text = balance;
                                                 
                                                 NSNumber * business = dataDic[@"business"];
                                                 self.insUsePartnerControl.valueLable.text = business.description;
                                                 
                                                 NSNumber * n_business = dataDic[@"business"];
                                                 self.insUsePartnerControl.valueLable.text = n_business.description;
                                                 
                                                 NSInteger allBusiness = balance.integerValue + n_business.integerValue;
                                                 self.allPatnerControl.valueLable.text = @(allBusiness).description;
                                                 
                                                 NSDictionary * area = dataDic[@"channel"];
                                                 NSString * finsh;
                                                 if ([area.allKeys containsObject:@"province"]) {
                                                     finsh = area[@"province"];
                                                 }
                                                 if ([area.allKeys containsObject:@"city"]) {
                                                     finsh = [NSString stringWithFormat:@"%@-%@",finsh,area[@"city"]];
                                                 }
                                                 if ([area.allKeys containsObject:@"county"]) {
                                                     finsh = [NSString stringWithFormat:@"%@-%@",finsh,area[@"county"]];
                                                 }
                                                 if ([area.allKeys containsObject:@"town"]) {
                                                     finsh = [NSString stringWithFormat:@"%@-%@",finsh,area[@"town"]];
                                                 }
                                                 if ([area.allKeys containsObject:@"name"]) {
                                                     finsh = [NSString stringWithFormat:@"%@-%@",finsh,area[@"name"]];
                                                 }
                                                 self.areaLable.text = finsh;
                                                 
                                                 [self reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                  [self reloadData];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}
@end
